package org.zxp.designpattern.agent.dy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @program: effectiveJava
 * @description:动态代理类生成工具
 * @author: X-Pacific zhang
 * @create: 2019-08-27 16:01
 **/
public class UserDaoDynaProxy<T> implements InvocationHandler {
    private T target;

    public T getProxy(T tartget) {
        this.target = tartget;
        return (T)Proxy.newProxyInstance(target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),this);
    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object ret = null;
        System.out.println("动态代理插入前");
        ret = method.invoke(target, args);
        System.out.println("动态代理插入后");
        return ret;
    }
}
